package com.huanying.framework.utils;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huanying.framework.user.User;
import com.huanying.framework.user.UserService;
import com.huanying.goodjob.job.JobService;
import com.huanying.goodjob.job.Jobitem;
import com.huanying.goodjob.job.Jobitem_User;
import com.huanying.goodjob.job.Jobitemstatus_Change;
import com.huanying.goodjob.message.Message;
import com.huanying.goodjob.message.MessageService;

@Controller
public class Test implements Serializable {

	private static final long serialVersionUID = 7121730466998892016L;
	private static Logger logger = Logger.getLogger(Test.class);

	@Autowired
	private JobService jobService;
	@Autowired
	private UserService userService;
	@Autowired
	private MessageService messageService;

	@RequestMapping(value = "/test.do", produces = "application/json; charset=utf-8")
	@ResponseBody
	public void test() {
		/*
		 * Message message = new Message(); message.setMessage("接收到一条推送");
		 * String push = "0"; try { push =
		 * PushtoSingle.pushApp("3c882f2c016ef9249ef0c58b2cb8319c", message); }
		 * catch (Exception e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } return push;
		 */

		/*
		 * MemCachedManager cache = MemCachedManager.getInstance(); long
		 * begintime = System.currentTimeMillis(); for(int i = 0; i < 1000;
		 * i++){ cache.set(String.valueOf(i),111); } long
		 * endtime=System.currentTimeMillis();
		 * 
		 * long costTime = (endtime - begintime);
		 * 
		 * return String.valueOf(costTime);
		 */

		// 处理时间
		SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
		Calendar rightNow = Calendar.getInstance();
		rightNow.setTime(new Date());
		rightNow.add(Calendar.DAY_OF_MONTH, -1);
		Date dt2 = rightNow.getTime();
		String end_request_date_2 = format.format(dt2);
		rightNow.add(Calendar.HOUR_OF_DAY, -1);
		Date dt1 = rightNow.getTime();
		String end_request_date_1 = format.format(dt1);
		int count = 0;
		try {
			Map<String, String> map = new HashMap<String, String>();
			Jobitemstatus_Change jobitemstatus_change = new Jobitemstatus_Change();
			Jobitem_User jobitem_user = null;
			map.put("jobitem_status", "4");
			map.put("end_request_date_1", end_request_date_1);
			map.put("end_request_date_2", end_request_date_2);
			map.put("status", "1");
			List<Object> list = jobService.searchJobitems(map);
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					Jobitem jobitem = (Jobitem) list.get(i);
					map.put("jobitem_status", "1");
					map.put("jobitem_id", String.valueOf(jobitem.getId()));
					List<Object> list1 = jobService.searchJobitem_Users(map);
					if (list1 != null) {
						jobitem_user = (Jobitem_User) list1.get(0);

						// 添加用户的bean
						User user = jobitem_user.getUser();
						user.setBeans(user.getBeans() + 1);
						userService.update(user);

						// 发送消息
						Message message = new Message();
						User receiver = userService.getbyId(jobitem_user.getUser().getId());
						User sender = userService.getbyId(jobitem.getJob().getUser().getId());
						message.setSender(sender);
						message.setReceiver(receiver);
						message.setJobitem(jobitem);
						message.setStatus(1);
						message.setIssystemmessage(1);
						message.setMessage("工长已确认完工");
						message.setCheckstatus(0);
						messageService.add_message(message);
						// 推送
						PushtoSingle.pushApp(receiver.getClientid(), message);
					}

					// 修改工作项状态
					jobitem.setJobitem_status(-2);
					jobService.update_jobitem(jobitem);
					// 添加工作项状态日志
					jobitemstatus_change.setJobitem(jobitem);
					jobitemstatus_change.setJobitem_status(-2);
					jobitemstatus_change.setTime(new Date());
					jobitemstatus_change.setStatus(1);
					jobService.add_jobitemstatus_change(jobitemstatus_change);
					// 次数记录
					count++;
				}
			}
			logger.info("扫描时间为：" + end_request_date_1 + "到" + end_request_date_2);
			logger.info("本次扫描共实现" + count + "条数据的状态变化。");
		} catch (Exception e) {
			logger.error("自动确认完工失败", e);
		}
	}
}
